penaltygame = {};
penaltygame.SHOOT = 'SHOOT';
penaltygame.CATCH = 'BLOCK';
penaltygame.actionPerformed = {name: penaltygame.SHOOT, value:1};

penaltygame.setVisibility = function(id, visible) {
	if (id instanceof Array) {
		for (idx in id) {
	       var e = document.getElementById(id[idx]);
	       e.style.visibility = visible ? 'visible' : 'hidden';
		}
	} else {
       var e = document.getElementById(id);
       e.style.visibility = visible ? 'visible' : 'hidden';
	}
}

penaltygame.selectBall = function(obj, selected, pos) {
        if (penaltygame.actionPerformed && (pos == penaltygame.actionPerformed.value)) {
          return;
        }
				if (selected) {
					// TODO(rbajales): Move this url reference out of the js
					obj.src = "http://penaltyshoots.googlecode.com/svn/branches/dev/ball7.gif";
					if (penaltygame.actionPerformed.name == penaltygame.CATCH) {
						if (pos == 1 || pos == 4) {
							document.getElementById('goalkeeper').innerHTML = '<img src="http://penaltyshoots.googlecode.com/svn/branches/dev/Img00018.png"/>';
						} else if (pos == 3 || pos == 6) {
							document.getElementById('goalkeeper').innerHTML = '<img src="http://penaltyshoots.googlecode.com/svn/branches/dev/Img00024.png"/>';
						} else {
							document.getElementById('goalkeeper').innerHTML = '<img src="http://penaltyshoots.googlecode.com/svn/branches/dev/Img00054.png"/>';
						}
					}
				} else {
					obj.src = "http://penaltyshoots.googlecode.com/svn/branches/dev/ball5.gif";
					if (penaltygame.actionPerformed.name == penaltygame.CATCH) {
						document.getElementById('goalkeeper').innerHTML = '<img src="http://penaltyshoots.googlecode.com/svn/branches/dev/Img00054.png"/>';
					}
				}
}

penaltygame.returnToMain = function() {
	penaltygame.setVisibility('main', true);
	penaltygame.setVisibility('playground', false);
	penaltygame.setVisibility('ballRunning', false);
	penaltygame.setVisibility('pos' + penaltygame.actionPerformed.value, false);
	penaltygame.setVisibility('returnToMain', false);
}

penaltygame.get = function(id) {
	return document.getElementById(id);
}

penaltygame.goToPlayGround = function(actionToPerform, theFriend) {
	penaltygame.actionPerformed.name = actionToPerform;
	if (penaltygame.actionPerformed.name == penaltygame.SHOOT) {
		penaltygame.get('message').innerHTML = 'Click on desired ball and press Shoot! button...';
		penaltygame.get('challengeInfo').innerHTML = 'You have challenged to ' + theFriend;
		penaltygame.get('actionButton').innerHTML = 'Shoot!';
	} else {
		penaltygame.get('message').innerHTML = 'Click on desired ball and press Catch! button...';
		penaltygame.get('challengeInfo').innerHTML = 'You have to catch the shoot from ' + theFriend;
		penaltygame.get('actionButton').innerHTML = 'Catch!';
	}
	penaltygame.setVisibility('main', false);
	penaltygame.setVisibility('playground', true);
	penaltygame.setVisibility(['challengeInfo', 'action', 'message', 'ball', 'pos1', 'pos2', 'pos3', 'pos4', 'pos5', 'pos6'], true);
}

penaltygame.performAction = function() {
	penaltygame.setVisibility(['challengeInfo', 'action', 'message', 'ball', 'pos1', 'pos2', 'pos3', 'pos4', 'pos5', 'pos6'], false);
	penaltygame.setVisibility('ballRunning', true);
	penaltygame.setVisibility('pos' + penaltygame.actionPerformed.value, true);
	penaltygame.setVisibility('returnToMain', true);
  storeAction(penaltygame.actionPerformed.name, penaltygame.friendId, penaltygame.actionPerformed.value, penaltygame.shotId);
}

penaltygame.selectActionPerformed = function(pos) {
  if (penaltygame.actionPerformed.value) {
    document.getElementById("pos" + penaltygame.actionPerformed.value).getElementsByTagName("img")[0].src = "http://penaltyshoots.googlecode.com/svn/branches/dev/ball5.gif";
    }
  penaltygame.actionPerformed.value = pos;
  penaltygame.selectBall(this, true, pos);
}